package plus.H5D3B5016;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import plus.H5D3B5016.view.LoadingView;

public class BaseActivity extends AppCompatActivity {

    protected App app;

    protected LoadingView loading;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try{
            x.view().inject(this);
            EventBus.getDefault().register(this);

            app=(App)getApplication();
            loading=new LoadingView(this,R.style.CustomDialog);


        }catch (Exception e){}
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    /**
     * 发送 POST
     * @param request
     * @param myCall
     */
    protected void post(Request request, final MyCall myCall){

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(3, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .build();

        loading.show();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(loading.isShowing()){
                            loading.dismiss();
                        }
                        show("网络错误");
                    }
                });
            }


            @Override
            public void onResponse(Call call,final Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if(loading.isShowing()){
                                loading.dismiss();
                            }

                            if(response.code()!=200){
                                show("网络错误:"+response.code());
                                return;
                            }

                            if(null!=myCall){
                                myCall.onResponse(response.body().string());
                            }

                        }catch (Exception e){}
                    }
                });


            }
        });
    }


    protected void show(final String text){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this,text,Toast.LENGTH_SHORT).show();
            }
        });
    }



    public interface MyCall{
        void onResponse(final String response);
    }


}
